﻿Imports System.Data.OleDb

Public Class SelecteerLeveringForm

#Region "Private declarations"

    Private _databaseBestand As String

#End Region

#Region "Public property's"

    Public ReadOnly Property IdLevering() As Integer
        Get
            If (Grid1.SelectedRows.Count = 1) Then
                Dim gridRow As DataGridViewRow = Grid1.SelectedRows(0)
                Dim dataRow As ArtikelsDataSet.LeveringenRow = DirectCast(DirectCast(gridRow.DataBoundItem, DataRowView).Row, ArtikelsDataSet.LeveringenRow)
                If (dataRow IsNot Nothing) Then
                    Return dataRow.Id
                Else
                    Return 0
                End If
            Else
                Return 0
            End If
        End Get
    End Property

#End Region

#Region "Constructor/Initialisatie"

    Public Sub New(ByVal databaseBestand As String)
        Me.InitializeComponent()
        _databaseBestand = databaseBestand
        Me.BindingNavigatorAddNewItem.Visible = False
        Me.BindingNavigatorSaveItem.Visible = False
        Me.BindingNavigator1.AddNewItem = Nothing
        Me.BindingNavigator1.DeleteItem = Nothing
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        Try
            Dal.LeveringOphalen(Me.ArtikelsDataSet1, _databaseBestand)
            InitGrid()
        Catch ex As Exception
            MsgBox.ShowError("Fout tijdens het inladen van de gegevens")
            log.WriteError("SelecteerLeveringForm_Load" & Environment.NewLine & ex.ToString())
        End Try
    End Sub

#End Region

#Region "Event handling"

    Private Sub tsbSluiten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub Grid1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Grid1.SelectionChanged
        btnOK.Enabled = (Grid1.SelectedRows.Count = 1)
    End Sub

    Private Sub Grid1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Grid1.CellDoubleClick
        Me.DialogResult = Windows.Forms.DialogResult.OK ' we hebben een selectie
        Me.Close()
    End Sub

#End Region

#Region "Helperfuncties"

    Private Sub InitGrid()
        With Grid1
            .ReadOnly = True
            With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Leveringen.IdColumn.ColumnName, Grid1)
                .Visible = False ' Autonumber niet tonen
            End With
            With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Leveringen.DatumColumn.ColumnName, Grid1)
                .Width = 100
            End With
            With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Leveringen.IdLeverancierColumn.ColumnName, Grid1)
                .HeaderText = "Leverancier"
                .Width = 250
                Dim column As DataGridViewComboBoxColumn = DirectCast(DataGridViewHelper.GetGridColumnByDataName _
                    (ArtikelsDataSet1.Leveringen.IdLeverancierColumn.ColumnName, Grid1), DataGridViewComboBoxColumn)
                column.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing ' verberg de uitklappijl
            End With
            With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Leveringen.FactuurnummerColumn.ColumnName, Grid1)
                .HeaderText = "Leveringsnummer"
                .Width = 150
            End With
        End With
    End Sub

#End Region

End Class